<?php

namespace Sbm\ControlPanel;

use Zoggi\Database;
use Sbm;

require("../bootstrap.php");



// подключаемся к базе данных
$db = new Database\Mysql();
$db->SetConfig($cfg['db']);
$db->SetTables($cfg['tables']);
$db->Connect();

//{{{ Паараметры от пользователя

$query = isset($_GET['query']) ? $_GET['query'] : '';
$table = isset($_GET['table']) ? $_GET['table'] : '';
$type = isset($_GET['type']) ? $_GET['type'] : '';


//}}}
// Результаты поиска
$search_result = array();
$search_data = array();
$result = (object) array(
			'query' => $query,
			'suggestions' => &$search_result,
			'data' => $search_data,
);

if ( !$query )
{
	print json_encode($result);
}


switch ($table)
{
	case 'region':
		$search = new Sbm\RegionSearch($db);
		$list = $search->GetByFilters(array(
			'where' => "`region`.`title` LIKE '%" . $query . "%'"
				));
		break;
	case 'profession':
		$search = new Sbm\ProfessionSearch($db);
		$list = $search->GetByFilters(array(
			'where' => "`profession`.`title` LIKE '%" . $query . "%'"
				));
		break;
	case 'equipment':
		$search = new Sbm\EquipmentSearch($db);
		$list = $search->GetByFilters(array(
			'where' => "`equipment`.`title` LIKE '%" . $query . "%'"
				));
		break;
	case 'job':
		$search = new Sbm\JobSearch($db);
		$list = $search->GetByFilters(array(
			'where' => "`job`.`title` LIKE '%" . $query . "%'"
				));
		foreach ((array) $list as $k => $v)
		{
			$search_data[] = (float) $v->price;
		}
		break;
}
if ( $list )
{
	foreach ($list as $k => $v)
	{
		$search_result[] = $v->GetName();
	}
}

print json_encode($result);